function singleQueue() {
    return function (
        target: any,
        propertyKey: string,
        descriptor: PropertyDescriptor
    ) {
        // 获取原函数
        const originalMethod = descriptor.value;

        // 定义新函数
        const newMethod = async function (...args: any[]) {
            // 定义返回结果
            let result: any;

            // 获取单例实例
            const instance = Singleton.getInstance();

            // 将请求方法放入请求队列
            instance.requestQueue.push(async () => {
                // 如果队列中只有一个请求，说明之前没有请求或者请求已经处理完了，直接执行请求
                if (instance.requestQueue.length === 1) {
                    try {
                        result = await originalMethod.apply(this, args);
                    } catch (error) {
                        console.error(error);
                        instance.isProcessing = false;
                        throw error;
                    }

                    // 执行队列中的其他请求
                    instance.processQueue();
                }
                // 如果队列中有多个请求，说明之前已经有请求正在处理，当前请求进入等待队列
                else {
                    await new Promise((resolve, reject) => {
                        instance.waitingQueue.push({ resolve, reject });
                    });
                    // 执行当前请求
                    try {
                        result = await originalMethod.apply(this, args);
                    } catch (error) {
                        console.error(error);
                        instance.isProcessing = false;
                        throw error;
                    }

                    // 执行等待队列中的其他请求
                    instance.processWaitingQueue();
                }
            });

            return result;
        };

        descriptor.value = newMethod;

        return descriptor;
    };
}

// 单例模式类
class Singleton {
    private static instance: Singleton;
    public isProcessing = false;
    public requestQueue: (() => Promise<any>)[] = [];
    public waitingQueue: { resolve: any; reject: any }[] = [];

    private constructor() { }

    public static getInstance() {
        if (!Singleton.instance) {
            Singleton.instance = new Singleton();
        }
        return Singleton.instance;
    }

    public async processQueue() {
        this.isProcessing = true;
        while (this.requestQueue.length > 0) {
            const request = this.requestQueue[0];
            try {
                await request();
            } catch (error) {
                console.error(error);
            }
            this.requestQueue.shift();
        }
        this.isProcessing = false;

        // 如果等待队列中有请求，将等待队列中的请求转移到请求队列中
        if (this.waitingQueue.length > 0) {
            const waitingItem = this.waitingQueue.shift();
            this.requestQueue.push(async () => {
                waitingItem.resolve();
            });
            this.processQueue();
        }
    }

    public async processWaitingQueue() {
        while (this.waitingQueue.length > 0) {
            const waitingItem = this.waitingQueue[0];
            try {
                await waitingItem.resolve();
            } catch (error) {
                console.error(error);
            }
            this.waitingQueue.shift();
        }

        this.isProcessing = false;

        // 如果请求队列中有请求，将请求队列中的请求转移到等待队列中
        if (this.requestQueue.length > 0) {
            const request = this.requestQueue.shift();
            this.waitingQueue.push({
                resolve: async () => {
                    try {
                        await request();
                        this.processQueue();
                    } catch (error) {
                        console.error(error);
                    }
                },
                reject: null,
            });
        }
    }
}

export { singleQueue };

